package com.chinacoal.microservice.impl.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.api.OrgOpenClient;
import com.chinacoal.microservice.exception.MessageException;
import com.chinacoal.microservice.impl.mapper.CatalogMapper;
import com.chinacoal.microservice.impl.mapper.ProcessMapper;
import com.chinacoal.microservice.impl.service.ProcessService;
import com.chinacoal.microservice.impl.service.WorkFlowService;
import com.chinacoal.microservice.impl.util.ProcessFun;
import com.chinacoal.microservice.model.organization.vo.EmployeeVO;
import com.chinacoal.microservice.model.wofkflow.QueryProcessParam;
import com.chinacoal.microservice.model.wofkflow.entity.CatalogEntity;
import com.chinacoal.microservice.model.wofkflow.entity.ProcessEntity;
import com.chinacoal.microservice.model.wofkflow.resource.WrapAttribute;
import com.chinacoal.microservice.model.wofkflow.resource.WrapProcessCatalog;
import com.chinacoal.microservice.util.result.Result;
import com.chinacoal.microservice.util.tool.Tool;
import com.eos.workflow.api.IBPSServiceClient;
import com.eos.workflow.data.WFActivityDefine;

/**  
 * @Package: com.chinacoal.microservice.impl.service.impl.ProcessServiceImpl
 * @ClassName: ProcessServiceImpl
 * @ClassDesc: 流程ServiceImpl
 * @Createby: sunh
 * @CreatedDate: 2019-12-03 15:17:02
 */ 
@Service
@Configuration
public class ProcessServiceImpl implements ProcessService {
	
	@Resource
	private CatalogMapper catalogMapper;
	@Resource
	private ProcessMapper processMapper;
	@Resource
	private WorkFlowService workFlowService;
	@Resource
	private OrgOpenClient orgOpenClient;
	
	@Value("${eos.application.sys-code}")
	private String flowChart = "";
	
	
	@Override
	public Page<CatalogEntity> getProcessCatalog(Integer page, Integer pageSize, QueryProcessParam queryParam) throws Exception{
		
		Integer userId = queryParam.getUserId();
		Integer empId = getEmpByUserId(userId);
		
		queryParam.setAuthType("0");
		queryParam.setPartId(empId.toString());
		
		Page<CatalogEntity> pages = ProcessFun.getPage(page, pageSize);
		
		List<CatalogEntity> list = catalogMapper.findList(pages, queryParam);
		
		return pages.setRecords(list);
	}

	@Override
	public Page<ProcessEntity> getProcessList(Integer page, Integer pageSize, QueryProcessParam queryParam) throws Exception{

		Integer userId = queryParam.getUserId();
		Integer empId = getEmpByUserId(userId);
		
		queryParam.setAuthType("0");
		queryParam.setPartId(empId.toString());
		
		Page<ProcessEntity> pages = ProcessFun.getPage(page, pageSize);
		
		List<ProcessEntity> list = processMapper.findList(pages, queryParam);
		
		return pages.setRecords(list);
	}

	@Override
	public List<WFActivityDefine> getProcessActivity(IBPSServiceClient client, Long processId) throws Exception {

		if(processId == null) return new ArrayList<WFActivityDefine>();
		
		return client.getDefinitionQueryManager().queryActivitiesOfProcess(processId);
	}

	@Override
	public WrapProcessCatalog getProcessTree(Integer userId, String name, Long processId, String parentCatalogId) throws Exception {

		WrapProcessCatalog wrapProcessCatalog = new WrapProcessCatalog();
		
		QueryProcessParam queryParam = new QueryProcessParam();
		queryParam.setParentCatalogId(parentCatalogId);		// 父目录id
		queryParam.setProcessId(processId);					// 流程id
		
		Integer empId = getEmpByUserId(userId);
		
		queryParam.setAuthType("0");						// 人员类型
		queryParam.setPartId(empId.toString());				// 人员id
		
		
		if(StringUtils.isEmpty(parentCatalogId) && processId == null) {
			// 第一层的目录
			queryParam.setParentCatalogId("1");
			List<CatalogEntity> catalogList = catalogMapper.findList(ProcessFun.getPage(0, 0), queryParam);
			wrapProcessCatalog.setCatalogList(catalogList);
		}else if(StringUtils.isEmpty(parentCatalogId) && processId != null) {
			// 获取工作流服务
			IBPSServiceClient client = workFlowService.getIBPSServiceClient(empId,name);  
			// 获取流程下的节点活动
			List<WFActivityDefine> list = getProcessActivity(client, processId);
			List<WrapAttribute> activityList = ProcessFun.getOptionListWAD(list,true,null);
			wrapProcessCatalog.setActivityList(activityList);
		}else {
			// 指定目录下的流程和子目录
			queryParam.setCurrentState("3");
			List<CatalogEntity> catalogList = catalogMapper.findList(ProcessFun.getPage(0, 0), queryParam);
			List<ProcessEntity> processList = processMapper.findList(ProcessFun.getPage(0, 0), queryParam);
			wrapProcessCatalog.setCatalogList(catalogList);
			wrapProcessCatalog.setProcessList(processList);
		}
		
		// 生成树
		wrapProcessCatalog.setProcessTree();
		
		return wrapProcessCatalog;
	}
	
	/**  
	 * @Package: com.chinacoal.microservice.impl.service.impl.WorkFlowServiceImpl.getEmpByUserId
	 * @MethodName: getEmpByUserId
	 * @MethodDesc: 获取员工Id
	 * @ParamList: {
	 *				 @param userId
	 *			   }
	 * @Return: Integer
	 * @Createby: sunh
	 * @CreatedDate: 2019-12-03 15:20:09
	 */ 
	private Integer getEmpByUserId(Integer userId) throws Exception{
		
		Result<EmployeeVO> empResult = orgOpenClient.getEmpDetial(userId, null);
		if(empResult == null || empResult.getData() == null || empResult.getData().getEmpId() == null) {
			throw new MessageException("当前用户没有对应的员工");
		}
		
		Integer empId = empResult.getData().getEmpId();
		
		return Tool.toInteger(empId.toString());
	}

	@Override
	public String getFlowChart(String instanceId) throws Exception {
		String flowChartUrl = flowChart + "?processInstID=" + instanceId;
		return flowChartUrl;
	}
}
